package com.mason.forum.service.impl;

import com.mason.forum.common.ResultCode;
import com.mason.forum.dao.ArticleReplyMapper;
import com.mason.forum.exception.ApplicationException;
import com.mason.forum.model.Article;
import com.mason.forum.model.ArticleReply;
import com.mason.forum.model.vo.ArticleReplyVO;
import com.mason.forum.service.IArticleReplyService;
import com.mason.forum.service.IArticleService;
import com.mason.forum.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-07
 * Time: 22:05
 */

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {


    @Resource
    private ArticleReplyMapper articleReplyMapper;

    @Resource
    private IArticleService articleService;

    @Override
    public void replyArticle(ArticleReply articleReply) {
        // 参数校验
        if(articleReply == null || articleReply.getArticleId() == null ||
                articleReply.getPostUserId() == null || !StringUtils.hasLength(articleReply.getContent())) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        // 文章是否有效
        Article article = articleService.selectById(articleReply.getArticleId());
        if (article == null) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        if (article.getState() == 1 || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_BANNED);
        }
        // 设置默认值
        //一些为null就行，一些再controller层就会赋值，在service无从而知！
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReply.setLikeCount(0); // 点赞数
        articleReply.setDeleteState((byte) 0);
        articleReply.setState((byte) 0);
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);
        // dao - 创建一条回复
        int rows = articleReplyMapper.insertSelective(articleReply);
        if(rows != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(ResultCode.FAILED_CREATE);
        }
        // dao - 帖子回复数+1
        articleService.incrementReplyCount(articleReply.getArticleId());
        // 新增成功
        log.info(ResultCode.SUCCESS.toString() + "新增成功: art reply id: "
                + articleReply.getId() + ". ");


    }

    @Override
    public List<ArticleReplyVO> selectRepliesById(Long articleId) {
        // 参数校验
        if(articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        // 2. dao层 - 获取帖子详情
        Article article = articleService.selectById(articleId);
        if(article == null) {
            //帖子不存在
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(article.getDeleteState() == 1 || article. getState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_BANNED);
        }
        // 3. dao层 - 获取帖子列表
        List<ArticleReplyVO> articleReplyVOS = articleReplyMapper.selectRepliesById(articleId);
        // 直接返回
        return articleReplyVOS;
    }
}
